﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Schedule.UI.ScheduleService;
using Schedule.UI.Foundation;
using System.Windows.Input;

namespace Schedule.UI.ViewModel
{
    public class SubjectViewModel : INotifyPropertyChanged
    {
        public SubjectViewModel()
        {
            RefreshMonhoc();
            this.PropertyChanged += (s, e) => {
                if (insertCurMonhoc != null)
                    insertCurMonhoc.RaiseCanExecuteChanged();
                if (saveCurMonhoc != null)
                    saveCurMonhoc.RaiseCanExecuteChanged();
                if (delCurMonhoc != null)
                    delCurMonhoc.RaiseCanExecuteChanged();
            };
            
        }
        #region Khai bao
        public ScheduleServiceClient ServiceClient = new ScheduleServiceClient();
        #region propchange
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        private IEnumerable<MONHOC> monhocs;
        public IEnumerable<MONHOC> Monhocs
        {
            get { return this.monhocs; }
            set
            {
                this.monhocs = value;
                OnPropertyChange("Monhocs");
            }
        }


        private MONHOC currentMonhocs= new MONHOC();
        public MONHOC CurrentMonhocs
        {
            get { return this.currentMonhocs; }
            set
            {
                this.currentMonhocs = value;
                OnPropertyChange("CurrentMonhocs");
            }
        }
        //private IEnumerable<LOAIPHONG> loaiphongs;
        //public IEnumerable<LOAIPHONG> Loaiphongs
        //{
        //    get { return this.loaiphongs; }
        //    set
        //    {
        //        this.loaiphongs = value;
        //        OnPropertyChange("Loaiphongs");
        //    }
        //}
        
        //private LOAIPHONG currentLoaiPhong = new LOAIPHONG();
        //public LOAIPHONG CurrentLoaiPhong 
        //{
        //    get { return this.currentLoaiPhong; }
        //    set
        //    {
        //        this.currentLoaiPhong = value;
        //        OnPropertyChange("CurrentLoaiPhong");
        //    }
        //}
       
        #endregion

        #region refresh
        private void RefreshMonhoc()
        {
            this.ServiceClient.getAllMonhocCompleted += (s, e) =>
            {
                this.Monhocs = e.Result;
            };
            this.ServiceClient.getAllMonhocAsync();
        }

        //private void RefreshCompetitor()
        //{
        //    //throw new NotImplementedException();
        //    this.ServiceClient.getAllLoaiPhongCompleted += (s, e) =>
        //    {
        //        this.loaiphongs = e.Result;
        //    };
        //    this.ServiceClient.getAllLoaiPhongAsync();
            

        //}
        #endregion
        
        #region Save
        private RelayCommand saveCurMonhoc;
        public ICommand SaveCurMonhoc
        {
            get { return saveCurMonhoc ?? (saveCurMonhoc = new RelayCommand(() => SaveMonhoc(), () => this.currentMonhocs != null && this.currentMonhocs.ID != 0)); }
        }

        private void SaveMonhoc()
        {
            ServiceClient.saveMonhocAsync(currentMonhocs);
            RefreshMonhoc();
        }

        
        #endregion

        #region del
        private RelayCommand delCurMonhoc;
        public ICommand DelCurMonhoc
        {
            get { return delCurMonhoc ?? (delCurMonhoc = new RelayCommand(() => DelMonhoc(), () => this.currentMonhocs != null && this.currentMonhocs.ID != 0)); }
        }

        private void DelMonhoc()
        {
            ServiceClient.deleteMonhocAsync(currentMonhocs);
            RefreshMonhoc();
        }
        #endregion

        #region Insert

        private RelayCommand insertCurMonhoc;
        public ICommand InsertCurMonhoc
        {
            get { return insertCurMonhoc ?? (insertCurMonhoc = new RelayCommand(() => InsertMonhoc(), () => this.currentMonhocs != null)); }
        }

        private void InsertMonhoc()
        {
            ServiceClient.insertMonhocAsync(CurrentMonhocs);
            RefreshMonhoc();
        }
        #endregion
        
    }
}
